import SwiftUI

struct CartBottomBar: View {
    @Binding var isAllSelected: Bool
    let totalPrice: Double
    let totalCount: Int
    let isEditing: Bool
    
    var onAllSelectToggle: () -> Void
    var onCheckout: () -> Void
    var onDelete: () -> Void
    var onAddToFavorites: () -> Void
    
    var body: some View {
        VStack(spacing: 0) {
            Divider()
            
            HStack(spacing: 12) {
                // 全选按钮
                Button(action: onAllSelectToggle) {
                    HStack(spacing: 8) {
                        Image(systemName: isAllSelected ? "checkmark.circle.fill" : "circle")
                            .foregroundColor(isAllSelected ? .blue : .gray)
                        
                        Text("全选")
                            .font(.system(size: 14))
                            .foregroundColor(.primary)
                    }
                }
                
                Spacer()
                
                if isEditing {
                    // 编辑模式下显示删除和收藏按钮
                    HStack(spacing: 10) {
                        Button(action: onAddToFavorites) {
                            Text("收藏")
                                .font(.system(size: 16, weight: .medium))
                                .foregroundColor(.white)
                                .padding(.horizontal, 16)
                                .padding(.vertical, 10)
                                .background(totalCount > 0 ? Color.blue : Color.gray)
                                .cornerRadius(20)
                        }
                        .disabled(totalCount == 0)
                        
                        Button(action: onDelete) {
                            Text("删除")
                                .font(.system(size: 16, weight: .medium))
                                .foregroundColor(.white)
                                .padding(.horizontal, 16)
                                .padding(.vertical, 10)
                                .background(totalCount > 0 ? Color.red : Color.gray)
                                .cornerRadius(20)
                        }
                        .disabled(totalCount == 0)
                    }
                } else {
                    // 正常模式下显示价格和结算按钮
                    HStack(spacing: 12) {
                        VStack(alignment: .trailing, spacing: 2) {
                            HStack(alignment: .firstTextBaseline, spacing: 2) {
                                Text("合计:")
                                    .font(.system(size: 14))
                                    .foregroundColor(.secondary)
                                
                                Text("¥")
                                    .font(.system(size: 12, weight: .bold))
                                    .foregroundColor(.red)
                                
                                Text(String(format: "%.2f", totalPrice))
                                    .font(.system(size: 18, weight: .bold))
                                    .foregroundColor(.red)
                            }
                            
                            if totalCount > 0 {
                                Text("共\(totalCount)件商品")
                                    .font(.system(size: 12))
                                    .foregroundColor(.secondary)
                            }
                        }
                        
                        Button(action: onCheckout) {
                            Text("结算")
                                .font(.system(size: 16, weight: .medium))
                                .foregroundColor(.white)
                                .padding(.horizontal, 24)
                                .padding(.vertical, 10)
                                .background(totalCount > 0 ? Color.red : Color.gray)
                                .cornerRadius(20)
                        }
                        .disabled(totalCount == 0)
                    }
                }
            }
            .padding(.horizontal, 16)
            .padding(.vertical, 10)
            .background(Color.white)
            .padding(.bottom, 20) // 为底部安全区域添加额外的padding
        }
    }
}

struct CartBottomBar_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            Spacer()
            CartBottomBar(
                isAllSelected: .constant(true),
                totalPrice: 299.99,
                totalCount: 3,
                isEditing: false,
                onAllSelectToggle: {},
                onCheckout: {},
                onDelete: {},
                onAddToFavorites: {}
            )
        }
        .previewLayout(.sizeThatFits)
    }
}